<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="search">
        <el-form-item>
          <el-input v-model="search.key" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="商店名称" width="300" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="商店电话" width="220" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="177" sortable>
      </el-table-column>
      <el-table-column prop="state" label="商店状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state==1" style="color:yellow">待审核</span>
          <span v-else-if="scope.row.state==2" style="color: green">审核通过待激活</span>
          <span v-else-if="scope.row.state==3" style="color: greenyellow">激活成功</span>
          <span v-else-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="商店地址" width="120" sortable>
      </el-table-column>
      <!--      <el-table-column prop="logo" label="商店商标" width="120" sortable>-->
      <!--      </el-table-column>  -->
      <el-table-column label="商店商标" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.logo" width="120" height="120">
        </template>
      </el-table-column>

      <el-table-column label="操作" width="230">
        <template scope="scope">
          <el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" size="small" @click="handleExamine(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--
           el-pagination 表示分页
           @size-change : 每页条数改变的时候会触发的方法
           @current-change: 当页数改变的时候
           :current-page: 绑定当前页数据
           :page-sizes="[100, 200, 300, 400]" : 每页多少条的选择
           :page-size="100" 每页的条数
           :total="400" 总条数
           layout="total, sizes, prev, pager, next, jumper"
      -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="search.currentPage"
          :page-sizes="[5,10]"
          :page-size="100"
          style="float: right"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveFormVisible" :rules="formRules" :close-on-click-modal="false" class="demo-ruleForm">
      <el-form :model="saveForm" label-width="80px"  ref="addForm">
        <el-form-item label="商店名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="商店电话" prop="tel">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="saveForm.registerTime" auto-complete="off"></el-input>
        </el-form-item>
        <!--        <el-form-item label="商店地址" prop="address">-->
        <!--          <el-input v-model="saveForm.address" auto-complete="off"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item prop="address" label="店铺地址">
          <el-input type="text" v-model="saveForm.address" auto-complete="off" placeholder="请输入地址！"></el-input>
          <el-button size="small" type="primary" @click="selectAdrress">选择</el-button>
        </el-form-item>

        <el-form-item label="部门商标" prop="state">
          <el-input v-model="saveForm.logo" auto-complete="off"></el-input>
        </el-form-item>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" >提交</el-button>
      </div>
    </el-dialog>

    <el-dialog
        title="选择地址"
        :visible.sync="dialogVisable"
        width="30%">
      <baidu-map :center="{lng: 116.403765, lat: 39.914850}" :zoom="11">
        <bm-view class="map"></bm-view>
        <bm-control :offset="{width: '10px', height: '10px'}">
          <!--:sugStyle="{zIndex: 2100} 让搜索提示上浮-->
          <bm-auto-complete v-model="keyword" :sugStyle="{zIndex: 2100}">
            <div style="margin-bottom:10px">
              <input v-model="keyword" type="text" placeholder="请输入关键字" class="searchinput"/>
              <el-button type="success" @click="selectAdrressConfirm">确定</el-button>
            </div>
          </bm-auto-complete>
        </bm-control>
        <bm-local-search :keyword="keyword" :auto-viewport="true" ></bm-local-search>
      </baidu-map>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisable=false">取 消</el-button>
        <el-button type="primary" @click="dialogVisable=false">确 定</el-button>
      </span>
    </el-dialog>
    <!--审核界面-->
    <el-dialog title="审核" :visible.sync="examineFormVisible" :close-on-click-modal="false">
      <el-form :model="examineForm" label-width="80px"  ref="addForm">
        <el-form-item label="商店名称" prop="name" >
          <el-input v-model="examineForm.name" auto-complete="off" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商店电话" prop="intro">
          <el-input v-model="examineForm.tel" auto-complete="off" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" prop="intro">
          <el-input v-model="examineForm.registerTime" auto-complete="off" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商店地址" prop="intro">
          <el-input v-model="examineForm.address" auto-complete="off" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="部门商标" prop="state">
          <el-input v-model="examineForm.logo" auto-complete="off" disabled="true"></el-input>
        </el-form-item>



        <el-form-item label="错误类型" prop="errorType">
          <template>
            <el-select v-model="examineForm.errorType" placeholder="问题类型">
              <el-option
                  v-for="examineForm in options"
                  :key="examineForm.value"
                  :label="examineForm.label"
                  :value="examineForm.label">
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="具体原因" prop="errorMsg">
          <el-input v-model="examineForm.errorMsg" auto-complete="off"></el-input>
        </el-form-item>






      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.native="cancellationSubmit" >取消</el-button>
        <el-button type="primary" @click.native="passSubmit" >通过</el-button>
        <el-button type="primary" @click.native="rejectSubmit" >驳回</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
// import util from '../../common/js/util'
// //import NProgress from 'nprogress'
// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      keyword:'',
      options: [{
        value: '选项1',
        label: '涉黄'
      }, {
        value: '选项2',
        label: '设毒'
      }, {
        value: '选项3',
        label: '设赌'
      }, {
        value: '选项4',
        label: '涉嫌暴力'
      }, {
        value: '选项5',
        label: '其他'
      }],
      value:'',
      deptTree:[],
      employees:[],
      search: {
        key: '',
        pageSize: 5,
        currentPage: 1
      },
      pageInfo: {
        data: [],
        total: 0,
      },
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      dialogVisable:false,
      editFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },


      saveFormVisible: false,//新增界面是否显示
      examineFormVisible: false,//审核界面是否显示
      saveForm:{
        id:null,
        name:"",
        tel:"",
        registerTime:"",
        state: 1,
        address:"",
        logo:""
      },
      formRules: {
        name: [
          {required: true, message: '请输入店铺名称!', trigger: 'blur'}
        ],
        tel: [
          {required: true, message: '请输入店铺座机!', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入店铺地址!', trigger: 'blur'}
        ]
      },

      examineForm:{
        id:null,
        name:"",
        tel:"",
        registerTime:"",
        state: "",
        address:"",
        logo:"",
        errorType:"",
        errorMsg:"",
        user: ""
      },

    }
  },
  methods: {
    selectAdrress(){
      this.dialogVisable = true;
    },
    selectAdrressConfirm(){
      //获取值搜索框值,设置给地址
      // var searchInputV=document.getElementById("searchInput").value;
      this.saveForm.address = this.keyword;
      //关闭对话框
      this.dialogVisable = false;
    },
    // 点击新增
    handleAdd(){
      this.saveFormVisible = true;
      this.saveForm= {
        id:null,
        name:"",
        tel:"",
        registerTime:"",
        state: 1,
        address:"",
        logo:""
      }
    },
    // 点击查询的方法
    queryData(){
      this.search.currentPage = 1
      this.getDepartments()
    },
    //每页条数变动的时候
    handleSizeChange(val){
      this.search.pageSize = val;
      this.getDepartments();
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    handleCurrentChange(val) {
      this.search.currentPage = val;
      this.getDepartments();
    },
    //获取用户列表
    //获取用户列表
    getDepartments() {
      this.listLoading=true;
      this.$http.post("/shop",this.search)
          .then(res=>{
            this.listLoading=false;
            res = res.data;
            console.log(res)
            if(res.success){
              //this.departments = res.data.data
              // // this.total = res.data.total
              // this.total = res.data.total
              this.pageInfo = res.data

            }
          })
          .catch(res=>{})
      // this.listLoading = true;

    },
    handleExamine: function (index,row){
      this.examineFormVisible = true;
      this.examineForm = row;
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop/"+row.id)
            .then(res=>{
              res = res.data
              this.listLoading = false;
              if(res.success){
                this.$message({type:"success",message:"删除成功"});
              }else{
                this.$message.error("删除失败"+res.msg)
              }
              this.search.currentPage= 1;
              this.getDepartments();
            })
            .catch(res=>{})

      }).catch(() => {

      });
    },

    //显示新增界面
    handleSave: function (index,row) {
      this.saveFormVisible = true;
      this.saveForm = row;
      // var split = row.path.split("/");
      // var data = [];
      // for (let i = 1; i < split.length-1; i++) {
      //   data.push(Number.parseInt(split[i]))
      // }
      // this.saveForm.parent = data;
      // if(!row.parent){
      //   this.saveForm.parent={
      //     id:null,
      //     name:""
      //   }
      // }
      // if(!row.manager){
      //   this.saveForm.manager={
      //     id:null,
      //     username:""
      //   }
      // }
      // this.getManager();
      // this.getDeptTree()

    },
    //审核取消
    cancellationSubmit: function (){
      this.examineFormVisible = false;
    },
    //驳回

    rejectSubmit: function (){
      this.$confirm('确认驳回审核?', '提示', {
        type: 'warning'
      }).then(() => {
        this.examineForm.state=4;
        this.examineForm.user= JSON.parse(sessionStorage.getItem('user'));
        console.log(this.examineForm.errorType);
        let para = Object.assign({}, this.examineForm);
        console.log("---------------------");
        console.log(para);
        this.$http.put("/shop",para)
            .then(res=>{
              this.examineFormVisible = false;
              this.search.currentPage= 1;
              this.getDepartments();
            })
            .catch(res=>{})

      }).catch(() => {

      });

    },

    //审核通过
    passSubmit: function (){
      this.$confirm('确认审核通过?', '提示', {
        type: 'warning'
      }).then(() => {
        this.examineForm.state=2;
        this.examineForm.user= JSON.parse(sessionStorage.getItem('user'))
        let para = Object.assign({}, this.examineForm);
        console.log("=========================")
        console.log(para);
        this.$http.put("/shop",para)
            .then(res=>{
              this.examineFormVisible = false;
              this.search.currentPage= 1;
              this.getDepartments();
            })
            .catch(res=>{})

      }).catch(() => {

      });

    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {

            //NProgress.start();
            this.saveForm.state=1;
            let para = Object.assign({}, this.saveForm);
            this.saveForm.state=1;
            // var parent = para.parent;
            // if (parent.length>=0){
            //   if (parent,length=0){
            //     para.parent={id:null,name:""}
            //   }else {
            //     para.parent={id:parent[parent.length-1],name:""}
            //   }
            // }
            // 发送请求
            this.$http.put("/shop",para)
                .then(res=>{
                  this.saveFormVisible= false
                  this.$message({type:"success",message:"保存成功"});
                  this.getDepartments()
                })
                .catch(res=>{
                  this.saveFormVisible= false
                })

          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送请求
        this.$http.patch("/shop",ids)
            .then(res=>{
              res = res.data
              this.listLoading = false;
              if(res.success){
                this.$message({type:"success",message:"删除成功"});
              }else{
                this.$message.error("删除失败"+res.msg)
              }
              this.search.currentPage= 1;
              this.getDepartments();
            })
            .catch(res=>{})
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getDepartments();
  }
}

</script>
<style scoped>
.map {
  width: 100%;
  height: 300px;
}
.searchinput{
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
}
</style>